package com.repair.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.repair.client.PlatformClient;
import com.repair.client.dto.AdminInfoDto;
import com.repair.dao.RepairReportMapper;
import com.repair.dto.RepairReportDto;
import com.repair.entity.RepairReport;
import com.repair.exception.RepairInfoException;
import com.repair.exception.RepairReportException;
import com.repair.param.RepairReportAddParam;
import com.repair.param.RepairReportFindParam;
import com.repair.param.RepairReportUpdateParam;
import com.repair.service.RepairReportService;
import com.repair.util.PageInfo;
import com.repair.util.Result;
import com.repair.util.StateDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 报修项目表 服务实现类
 * </p>
 *
 * @author 小叶
 * @since 2024年04月03日
 */
@Service
public class RepairReportServiceImpl extends ServiceImpl<RepairReportMapper, RepairReport> implements RepairReportService {
    @Autowired
    private RepairReportMapper repairReportMapper;
    @Autowired
    private PlatformClient platformClient;

    @Override
    public RepairReport add(RepairReportAddParam param, Integer adminId) {
        //报修项目名称不能相同
        RepairReport repairReport = repairReportMapper.selectOne(new QueryWrapper<RepairReport>().eq("repair_name", param.getRepairName()));
        if (repairReport!=null) throw new RepairReportException(500,"报修项目名称已存在");
        //是否需要支付的状态只能是0或1
        if (param.getIsNeedPay()!=0&&param.getIsNeedPay()!=1){
            throw new RepairReportException(500,"支付传递参数错误");
        }
        //可以进行新增，查询出新增人的信息
        Result<AdminInfoDto> result = platformClient.findAdminById(adminId);
        if (result.getCode()!=200){
            throw new RepairInfoException(result.getCode(),result.getMessage());
        }
        RepairReport report = BeanUtil.toBean(param, RepairReport.class);
        report.setCreatePerson(result.getData().getAdminUser());
        report.setCreateTime(LocalDateTime.now().toString());
        report.setState(StateDate.REPAIR_REPORT_STATE_SHOW);
        repairReportMapper.insert(report);
        return report;
    }

    @Override
    public RepairReport updateMessage(RepairReportUpdateParam param) {
        //报修项目必须存在
        RepairReport repairReport = repairReportMapper.selectById(param.getRepairId());
        if (repairReport==null) throw new RepairReportException(500,"修改的报修项目不存在");
        //修改后的名称除了自己项目不能有相同的
        QueryWrapper<RepairReport> wrapper = new QueryWrapper<>();
        wrapper.eq("repair_name",param.getRepairName());
        wrapper.ne("repair_id",param.getRepairId());
        if (repairReportMapper.selectCount(wrapper)>0){
            throw new RepairReportException(500,"修改的名称已存在");
        }
        //是否付费的状态只能是0或1
        if (param.getIsNeedPay()!=0&&param.getIsNeedPay()!=1){
            throw new RepairReportException(500,"支付传递参数错误");
        }
        //如果需要付费，必须填写金额
        if (param.getIsNeedPay()==0){
            if (param.getPayMoney()==null||param.getPayMoney().compareTo(new BigDecimal(0))<=0){
                throw new RepairReportException(500,"付费金额不能为空");
            }
        }
        //如果不用付费,传递过来的金额必须为null或者0
        if (param.getIsNeedPay()==1){
            if (param.getPayMoney()!=null&&param.getPayMoney().compareTo(BigDecimal.ZERO)!=0){
                throw new RepairReportException(500,"不用付费金额");
            }
        }
        //可以进行修改
        repairReport.setRepairName(param.getRepairName());
        repairReport.setIsNeedPay(param.getIsNeedPay());
        if (param.getIsNeedPay()==0){
            //需要付费
            repairReport.setPayMoney(param.getPayMoney());
        }else{
            //不用付费
            repairReport.setPayMoney(BigDecimal.ZERO);
        }
        repairReportMapper.updateById(repairReport);
        return repairReport;
    }

    @Override
    public void delete(Integer repairId) {
        //判断删除的报修项目是否存在
        RepairReport repairReport = repairReportMapper.selectById(repairId);
        if (repairReport==null) throw new RepairReportException(500,"报修项目不存在");
        //删除的报修项目不能是已删除状态
        if (repairReport.getState()==StateDate.REPAIR_REPORT_STATE_DELETE){
            throw new RepairReportException(500,"该项目已被删除");
        }
        //可以进行删除，修改状态为已删除
        repairReport.setState(StateDate.REPAIR_REPORT_STATE_DELETE);
        repairReportMapper.updateById(repairReport);
    }

    @Override
    public RepairReport findById(Integer repairId) {
        //查询报修项目是否存在
        RepairReport repairReport = repairReportMapper.selectById(repairId);
        if (repairReport==null) throw new RepairReportException(500,"报修项目不存在");
        return repairReport;
    }

    @Override
    public PageInfo<RepairReportDto> findByCondition(RepairReportFindParam param) {
        //条件查询报修项目
        Page<RepairReport> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 5 : param.getPageSize());
        QueryWrapper<RepairReport> wrapper = new QueryWrapper<>();
        if (param.getRepairName()!=null&&param.getRepairName()!=""){
            wrapper.like("repair_name",param.getRepairName());
        }
        //如果传递了是否需要付费，是否需要付费的状态必须是0或1
        if (param.getIsNeedPay()!=null){
            if (param.getIsNeedPay()!=0&&param.getIsNeedPay()!=1){
                throw new RepairReportException(500,"传递的付费状态错误");
            }
            wrapper.eq("is_need_pay",param.getIsNeedPay());
        }
        wrapper.ne("state",StateDate.REPAIR_REPORT_STATE_DELETE);
        repairReportMapper.selectPage(page,wrapper);
        //将查询回来的数据进行转换
        List<RepairReport> records = page.getRecords();
        List<RepairReportDto> dtos = BeanUtil.copyToList(records, RepairReportDto.class);
        for (RepairReportDto dto : dtos) {
            if (dto.getIsNeedPay()==0){
                dto.setStateName("需要付费");
            }else{
                dto.setStateName("不需要付费");
            }
        }
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setList(dtos);
        pageInfo.setPages(page.getPages());
        return pageInfo;
    }
}
